﻿using AdminSystem.Application.Commands.lmgCommands;
using AdminSystem.Application.Dtos;
using AdminSystem.Domain.Entities;
using AdminSystem.EntityFramworkCore;
using AdminSystem.Repositories.Interfaces;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Application.CommandHandlers.lmgCommandHandlers
{
    public class lmgQueryCommandHandler : IRequestHandler<lmgQueryCommand, APIResult<List<ImgDto>>>
    {
        private readonly IBaseRepositories<Images> imagesBase;
        private readonly IBaseRepositories<ImagesMiddle> imagesMiddleBase;
        private readonly IBaseRepositories<Traceability> traceabilityBase;
        private readonly IBaseRepositories<Shop> shopBase;
        public lmgQueryCommandHandler(IBaseRepositories<Images> imagesBase, IBaseRepositories<ImagesMiddle> imagesMiddleBase, IBaseRepositories<Traceability> traceabilityBase, IBaseRepositories<Shop> shopBase)
        {
            this.imagesBase = imagesBase;
            this.imagesMiddleBase = imagesMiddleBase;
            this.traceabilityBase = traceabilityBase;
            this.shopBase = shopBase;
        }
        public async Task<APIResult<List<ImgDto>>> Handle(lmgQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<ImgDto>> result=new APIResult<List<ImgDto>>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();
            var list = from a in await imagesBase.GetAll()
                       join b in await imagesMiddleBase.GetAll() on a.ImgId equals b.ImgId
                       join c in await traceabilityBase.GetAll() on b.TheRestId equals c.Id
                       join d in await shopBase.GetAll() on b.TheRestId equals d.ShopId
                       where b.TheRestId == request.TheRestId
                       select new ImgDto
                       {
                           Id = a.ImgId,
                           Name = d.ShopName,
                           Url = a.ImgUrl,

                       };

            result.Data = list.ToList();

            return await Task.FromResult(result);
        }
    }
}
